package com.woniuxy.Test;

import com.woniuxy.bean.Person;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class MyBeanFactory {
    private static Map<String,String> map = new HashMap();
    static{
        //读取配置文件
        Properties properties = new Properties();
        //输入流读取配置文件
        InputStream inputStream = Test.class.getResourceAsStream("../util/person.properties");
        Person person = null;
        try {
            properties.load(inputStream);
            Set<String> keys = properties.stringPropertyNames();
            for (String name:keys) {
                String property = properties.getProperty(name);
                map.put(name,property);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  Object getBean(String name){
        String value = map.get(name);
        try {
            return Class.forName(value).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
